/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.sgsmcan.Sereno;

import com.sgsmcan.Dao.SerenoDao;
import com.sgsmcan.Dao.SerenoDaoImpl;
import com.sgsmcan.Entidad.Sereno;
import com.sgsmcan.Hibernate.HibernateUtil;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 *
 * @author GOMEZ
 */
public class TestSereno {
    
    static SerenoDao serenoDao;
    
    @BeforeClass
    public static void setUp(){
        serenoDao = new SerenoDaoImpl();
    }
    
    public void autoCompleteSereno(){
        try {
            List<Sereno> lst = serenoDao.autoComplete("a");
            System.out.println(lst.size());
            for (Sereno sereno : lst) {
                System.out.println("   "+sereno.getApellido()+ sereno.getDni());
            }
        } catch (Exception e) {
        }
    }
    
    @Test
    public void autoCompleteSerenoString(){
        List<String> list = null;
        Session session = null;
        String parametro="a";
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("SELECT CONCAT(se.nombre,' ',se.apellido) as fullname FROM Sereno as se WHERE se.apellido LIKE :parat");
            query.setParameter("parat", "%"+parametro+"%");
            query.setMaxResults(10);
            list = query.list();
            
            for (int i = 0; i <list.size(); i++) {
                System.out.println(list.get(i).toString()+"");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }
    
    
     @Test
    public void autoCompleteSerenoid(){
        List<Integer> list = null;
        Session session = null;
        String parametro="a";
        Throwable ex=null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("SELECT se.id FROM Sereno as se WHERE se.nombre LIKE :parat OR se.apellido LIKE :parat");
            query.setParameter("parat", "%"+parametro+"%");
            query.setMaxResults(10);
            list = query.list();
            
            for (int i = 0; i <list.size(); i++) {
                System.out.println(list.get(i)+"");
            }
        } catch (HibernateException e) {
            ex=e;
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
        Assert.assertNull(ex);
    }
    
    @Test
     public void testMostrarCodigo(){
         Assert.assertEquals(7, (int)(serenoDao.getSerenoId("a")));
     }    
    
}


